package cybertechcebu.task
{

	import cybertechcebu.Console;
	import cybertechcebu.FTPClient;
	import cybertechcebu.FTPCommand;
	import cybertechcebu.FTPResponse;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.Socket;


	[Event(name = "taskStart", type = "cybertechcebu.task.TaskEvent")]
	[Event(name = "taskEnd", type = "cybertechcebu.task.TaskEvent")]
	[Event(name = "taskProgress", type = "cybertechcebu.task.TaskEvent")]
	[Event(name = "taskError", type = "cybertechcebu.task.TaskEvent")]
	public class Task extends EventDispatcher
	{
		protected var _finished:Boolean = false;
		protected var _started:Boolean = false;
		protected var client:FTPClient;
		protected var dataSocket:Socket;
		
		public function Task(client:FTPClient)
		{
			this.client = client;
		}

		public function start():void
		{
			_started = true;
			dispatchEvent(new TaskEvent(TaskEvent.TASK_START));
			startTask();
		}

		protected function startTask():void
		{
			throw new Error("Virtual method");
		}

		public function handleResponse(response:FTPResponse):void
		{
			throw new Error("Virtual method");
		}

		protected function dispatchTaskEnd(result:Boolean):void
		{
			var evt:TaskEvent = new TaskEvent(TaskEvent.TASK_END);
			evt.result = result;
			dispatchEvent(evt);
			_finished = true;
		}

		protected function dispatchTaskProgress(progress:String):void
		{
			var evt:TaskEvent = new TaskEvent(TaskEvent.TASK_PROGRESS);
			evt.progress = progress;
			dispatchEvent(evt);
		}

		protected function dispatchTaskError(error:String):void
		{
			var evt:TaskEvent = new TaskEvent(TaskEvent.TASK_ERROR);
			evt.error = error;
			dispatchEvent(evt);
			_finished = true;
		}

		public function get started():Boolean
		{
			return _started;
		}

		public function get finished():Boolean
		{
			return _finished;
		}

		public function get type():String
		{
			return toString();
		}
		
		protected function sendCommand(command:FTPCommand):void{
			client.sendCommand(command);
		}
		
		protected function sendPassword(command:FTPCommand):void{
			client.sendPassword(command);
		}
		
		protected function addConnectHandler(handler:Function):void{
			dataSocket.addEventListener(Event.CONNECT, handler);
		}
		
		protected function addErrorHandler(handler:Function):void{
			dataSocket.addEventListener(IOErrorEvent.IO_ERROR, handler);
		}
		
		protected function addDataHandler(handler:Function):void{
			dataSocket.addEventListener(ProgressEvent.SOCKET_DATA, handler);
		}
		
		protected function addCloseHandler(handler:Function):void{
			dataSocket.addEventListener(Event.CLOSE, handler);
		}
	
		protected function initDataSocket():void{
			
			dataSocket = new Socket();
			
			dataSocket.addEventListener(Event.CONNECT, function(e:Event):void{
				Console.progress(toString() + " Data socket connected");
			});
			
			dataSocket.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void
			{
				var evt:TaskEvent = new TaskEvent(TaskEvent.TASK_ERROR);
				evt.error = e.toString();
				dispatchEvent(evt);
				_finished = true;
				
			});
			
			dataSocket.addEventListener(Event.CLOSE, function(e:Event):void
			{
				Console.progress(toString() + " Data socket closed");
			});
		}
		
		protected function connect(portString:String):void{
		
			var socketName:Object = portString.substring(portString.indexOf("(") + 1, portString.indexOf(")")).split(",");
			
			var host:String = socketName.slice(0, 4).join(".");
			
			var port:int = parseInt(socketName[4]) * 256 + int(socketName[5]);
			
			dataSocket.connect(host, port);
		}
	}
}
